<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增游戏附件')"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-file-add">
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">游戏id：</label>
                <div class="col-sm-8">
                    <select class="form-control noselect2 selectpicker" id="gameId" name="gameId">
                        <option th:each="game : ${games}" th:value="${game.id}" th:text="${game.name}">
                            --请选择游戏--
                        </option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">附件名称：</label>
                <div class="col-sm-8">
                    <input name="name" id="name" class="form-control" type="text" required>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">附件链接：</label>
                <div class="col-sm-8">
                    <input name="link" id="link" class="form-control" type="text" required>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label">附件类型：</label>
                <div class="col-sm-8">
                    <select id="type" name="type" class="form-control" th:with="type=${@dict.getType('biz_file_type')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label">是否为封面：</label>
                <div class="col-sm-8">
                    <select id="face" name="face" class="form-control" th:with="type=${@dict.getType('biz_file_face')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <textarea id="remark" name="remark" class="form-control"></textarea>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label">上传：</label>
                <div class="col-sm-8">
                    <input id="filePath" name="filePath" class="form-control" type="file">
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label">*两种方式：</label>
                <div class="col-sm-8" style="color: red">
                    <ul>
                        <li>方式1：直接找互联网的图片, 然后填写信息(不占自己资源)</li>
                        <li>方式2：点击上传, 选好游戏和格式再选择文件上传(占用自己服务器硬盘)</li>
                        <li>注意：以上方式二选一!</li>
                    </ul>
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">

    var prefix = ctx + "biz/file"
    $("#form-file-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        uploadFile();
    }


    function uploadFile() {
        var formData = new FormData();
        if ($('#filePath')[0].files[0] == null) {
            // $.modal.alertWarning("请先选择文件路径");
            // return false;
            // 没有选文件走普通提交
            // if ($.validate.form()) {
            //     $.operate.save(prefix + "/add", $('#form-file-add').serialize());
            // }
        } else {
            formData.append('file', $('#filePath')[0].files[0]);
        }

        // 其他表单内容
        formData.append('gameId', $("#gameId").val());
        formData.append('link', $("#link").val());
        formData.append('name', $("#name").val())
        formData.append('type', $("#type").val());
        formData.append('face', $("#face").val());
        formData.append('remark', $("#remark").val());

        $.ajax({
            url: prefix + "/add",
            type: 'post',
            cache: false,
            data: formData,
            processData: false,
            contentType: false,
            dataType: "json",
            success: function (result) {
                $.operate.successCallback(result);
            }
        });

    }
</script>
</body>
</html>